//
//  STImageHelper.swift
//  Story
//
//  Created by 汤泽川 on 2019/4/9.
//  Copyright © 2019 汤泽川. All rights reserved.
//

import Foundation
import UIKit

public extension UIImage {
    convenience init(color : UIColor, size : CGSize = CGSize(width: 10, height: 10)) {
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContext(size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(rect)
        let cgImage = (UIGraphicsGetImageFromCurrentImageContext()?.cgImage)!
        UIGraphicsEndImageContext()
        self.init(cgImage : cgImage)
    }
    
    static func imageFrom(view : UIView) -> UIImage {
        let size = view.bounds.size
        UIGraphicsBeginImageContextWithOptions(size, false, 0)
        view.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
    
}
